
--------------------------------------------------------------------------------

function Spawn( entityKeyValues )
	if not IsServer() then
		return
	end

	if thisEntity == nil then
		return
	end

	thisEntity.a1 = thisEntity:GetAbilityByIndex(0)
	thisEntity.a2 = thisEntity:GetAbilityByIndex(1)
	thisEntity.a3 = thisEntity:GetAbilityByIndex(2)
	thisEntity.a4 = thisEntity:GetAbilityByIndex(3)
	thisEntity:SetContextThink( DoUniqueString("bossThinker"), BossThink, 2 )
end

--------------------------------------------------------------------------------

function BossThink()
	if not IsServer() then
		return
	end

	if ( not thisEntity:IsAlive() ) then
		return -1
	end

	if GameRules:IsGamePaused() == true then
		return 0.5
	end
	
	local hEnemies = FindUnitsInRadius( thisEntity:GetTeamNumber(), thisEntity:GetOrigin(), nil, 5000, DOTA_UNIT_TARGET_TEAM_ENEMY, DOTA_UNIT_TARGET_HERO, DOTA_UNIT_TARGET_FLAG_MAGIC_IMMUNE_ENEMIES, FIND_CLOSEST, false )
	if #hEnemies == 0 then
		return 1
	end

	local hAttackTarget = nil
	local hApproachTarget = nil
	for _, hEnemy in pairs( hEnemies ) do
		if hEnemy ~= nil and hEnemy:IsAlive() and hEnemy:IsRealHero() then --and hEnemy:GetUnitName() ~= "npc_dota_friendly_bristleback_son" 
			local flDist = ( hEnemy:GetOrigin() - thisEntity:GetOrigin() ):Length2D()

			if flDist <= 600 then
				hAttackTarget = hEnemy
			end
			if flDist > 600 then
				hApproachTarget = hEnemy
			end
			-- if hEnemy.pz_time then
			-- 	if flDist <= 300 and thisEntity.a1 and thisEntity.a1:IsFullyCastable() then
			-- 		return CastAbility(thisEntity.a1)
			-- 	end
			-- end
		end
	end

	if hAttackTarget == nil and hApproachTarget ~= nil then
		return Approach( hApproachTarget )
	end
	--默认技能1是护盾，扫尾，捶地

	if hAttackTarget and thisEntity.a1 ~= nil and thisEntity.a1:IsFullyCastable() then
		return CastAbility(thisEntity.a1)
	end
	if hAttackTarget and thisEntity.a2 ~= nil and thisEntity.a2:IsFullyCastable() then
		return CastAbility(thisEntity.a2)
	end
	if hAttackTarget and thisEntity.a3 ~= nil and thisEntity.a3:IsFullyCastable() then
		return CastAbility(thisEntity.a3)
	end
	if hAttackTarget and thisEntity.a4 ~= nil and thisEntity.a4:IsFullyCastable() then
		return CastAbility(thisEntity.a4)
	end

	-- if hAttackTarget then
	-- 	thisEntity:FaceTowards( hAttackTarget:GetOrigin() )
	-- 	return 2.0
	-- end

	return 3
end

--------------------------------------------------------------------------------

--[[
	在即将释放技能的时候先释放出一个提示框的特效
]]

function CastAbility( ability )
	--print( "ai_bandit_archer - CastArrow" )
	local entindex1 = thisEntity:entindex()
	local entindex2 = ability:entindex()

	ExecuteOrderFromTable({
		UnitIndex = entindex1,
		OrderType = DOTA_UNIT_ORDER_CAST_NO_TARGET,
		AbilityIndex = entindex2,
		Queue = false,
	})

	return 2
end

--------------------------------------------------------------------------------

function Approach(unit)
	--print( "ai_bandit_archer - Approach" )

	local vToEnemy = unit:GetOrigin() - thisEntity:GetOrigin()
	vToEnemy = vToEnemy:Normalized()

	ExecuteOrderFromTable({
		UnitIndex = thisEntity:entindex(),
		OrderType = DOTA_UNIT_ORDER_MOVE_TO_POSITION,
		Position = thisEntity:GetOrigin() + vToEnemy * thisEntity:GetIdealSpeed()
	})

	return 2
end


